<?php

namespace App\Http\Controllers\Fee\FeishuApply;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\AppletAuth;
use App\Models\Fee\FeeAnnualOrderModel;
use App\Service\Fee\FeishuApply\FeeAnnualOrderService;
use App\Service\OpenShop\Feishu\PermissionService;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FeeAnnualOrderController extends BaseController
{
    public function headTitle(Request $request)
    {
        $userInfo = AppletAuth::$userInfo;
        $scope    = PermissionService::dataPermission(PermissionService::URI_SIGN_ORDER);
        return $this->success([
            'text' => FeeAnnualOrderService::headTitle($userInfo, $scope),
        ]);
    }


    public function countStatusNumber()
    {
        $userInfo = AppletAuth::$userInfo;
        $scope    = PermissionService::dataPermission(PermissionService::URI_SIGN_ORDER);
        return $this->success(FeeAnnualOrderService::countStatusNumber($userInfo, $scope));
    }


    public function list(Request $request)
    {
        $this->validate($request, [
            'patStatus' => 'numeric',
        ]);
        $userInfo  = AppletAuth::$userInfo;
        $scope     = PermissionService::dataPermission(PermissionService::URI_SIGN_ORDER);
        $pageNum   = (int)$request->get('pageNum', 1);
        $pageSize  = (int)$request->get('pageSize', 10);
        $param     = $request->get('param', '');
        $payStatus = $request->get('payStatus', -1);
        $status    = $request->get('status', '');
        return $this->success(FeeAnnualOrderService::getList($userInfo, $scope, $payStatus, $status, $param, $pageSize,
            $pageNum));
    }


    public function detail(Request $request, int $id)
    {
        return $this->success(FeeAnnualOrderService::detail($id));
    }

    public function feePayStatus(Request $request)
    {
        $shopIds = $request->get('shopIds');

        return $this->success(['pay' => FeeAnnualOrderService::feePayStatus($shopIds)]);
    }

    public function webLists(Request $request)
    {
        $feeOrderNo = (string)$request->get('feeOrderNo');
        $shopIds    = (string)$request->get('shopIds');
        $startTime  = (string)$request->get('startTime');
        $endTime    = (string)$request->get('endTime');
        $payStatus  = (string)$request->get('payStatus');
        $pageNum    = (int)$request->get('pageNum', 1);
        $pageSize   = (int)$request->get('pageSize', 20);

        $shopIds   = $shopIds ? explode(',', $shopIds) : [];
        $payStatus = $payStatus !== '' ? explode(',', $payStatus) : [];
        $startTime = $startTime ? strtotime($startTime) : 0;
        $endTime   = $endTime ? strtotime($endTime) : strtotime("99991231");
        $export    = $request->get('export', 0);

        // 如果要导出 时间不超过一年
        if ($export) {

            // 未传时间或者时间大于1年拒绝导出
            if (!$startTime || Carbon::createFromTimestamp($startTime)->addYear()->endOfDay()->timestamp < $endTime) {
                return $this->fail(401, '导出时时间应控制在1年');
            }
        }

        $data = FeeAnnualOrderService::webLists($feeOrderNo, $shopIds, $startTime, $endTime, $payStatus, $pageNum,
            $pageSize, $export);

        return $this->success($data);
    }

    public function delay(Request $request)
    {
        $feeOrderNo = $request->get('feeOrderNo');
        $day        = (int)$request->get('day');
        $date       = (string)$request->get('date');
        $remark     = $request->get('remark');
        $image      = $request->get('image', "");

        $this->validate($request, [
            'feeOrderNo' => 'required|string',
            'day'        => 'integer',
            'date'       => 'date',
            'remark'     => "required|string|min:1|max:1000",
            'image'      => "sometimes|url",
        ]);

        if (!$request->get('day') && !$request->get('date')) {
            $this->fail(1, 'day/date至少传一个');
        }

        $userId   = getRmsUserId();
        $userName = getRmsUserInfoByAuth()->username;

        return $this->success(FeeAnnualOrderService::delay(
            $feeOrderNo, $day, $date, $userId, $userName, $remark, $image
        ));
    }

    public function reduce(Request $request)
    {
        $feeOrderNo = $request->get('feeOrderNo');
        $price      = $request->get('price');
        $remark     = $request->get('remark');
        $image      = $request->get('image', "");

        $this->validate($request, [
            'feeOrderNo' => 'required|string',
            'price'      => 'required|integer|min:1',
            'remark'     => "required|string|min:1|max:1000",
            'image'      => "sometimes|url",
        ]);

        $userId   = getRmsUserId();
        $userName = getRmsUserInfoByAuth()->username;

        return $this->success(FeeAnnualOrderService::reduce($feeOrderNo, $price, $userId, $userName, $remark, $image));
    }

    public function getFeeTime(Request $request)
    {
        $shopIds = $request->get('shopIds');

        $this->validate($request, [
            'shopIds' => 'required|array',
        ]);

        return $this->success(FeeAnnualOrderService::getFeeTime($shopIds));
    }

    public function editFeeTime(Request $request)
    {
        $shopId = (int)$request->get('shopId');
        $date   = $request->get('date');

        $this->validate($request, [
            'shopId' => 'required|integer|min:1',
            'date'   => 'required|date',
        ]);

        $date     = date('Ymd', strtotime($date));
        $userId   = getRmsUserId();
        $userName = getRmsUserInfoByAuth()->username;

        FeeAnnualOrderService::editFeeTime($shopId, $date, $userId, $userName);

        return $this->success();
    }
}
